/*
 * Copyright (c) 2019 - 2020 IoT Company of Midea Group.
 *
 * File Name 	    : 
 * Description	    : rtos adaptor
 *
 * Version	        : v0.1
 * Author			: 
 * Date	            : 2019/09/15  refactoring
 * History	        : 
 */
 
#ifndef __MS_OSAL_THREAD_H__
#define __MS_OSAL_THREAD_H__

#include <stdint.h>
#include "ms_osal.h"

#ifdef __cplusplus
extern "C" {
#endif

#define MS_OSAL_THREAD_PRI_LOW     2
#define MS_OSAL_THREAD_PRI_NORMAL  1
#define MS_OSAL_THREAD_PRI_HIGH    0

/**
 * @brief	Create a new task and add it to the list of tasks that are ready to run
 * @param[in]	thandle : 		Used to pass back a handle by which the created task can be referenced
 * @param[in]	name : 		A descriptive name for the task
 * @param[in]	main_func : 	Pointer to the task entry function
 * @param[in]	arg : 			Pointer that will be used as the parameter for the task being created
 * @param[in]	stack_size : 	The size of the task stack
 * @param[in]	priority : 		The priority at which the task should run
 * @param[in]	autorun : 		whethe the task should run when be created, true : run it, false : not run
 * @return if successly, return MS_OSAL_RESULT_SUCCESS, \n
 *         otherwise return MS_OSAL_RESULT_ERROR
 **/
ms_osal_result_t ms_osal_thread_create(ms_osal_thread_t *thandle,
											const char *name,
		     								void (*main_func) (void *arg), 
		     								void *arg,
		     								uint32_t stack_size, 
		     								int32_t priority,
											uint8_t autorun);


/**
 * @brief	Remove a task from the RTOS kernels management
 * @param[in]	thandle : The handle of the task to be deleted. Passing NULL will cause the calling task to be deleted.
 * @return if successly, return MS_OSAL_RESULT_SUCCESS, \n
 *         otherwise return MS_OSAL_RESULT_ERROR
 **/
ms_osal_result_t ms_osal_thread_delete(ms_osal_thread_t *thandle);

/**
 * @brief	sleep a task for a given number of ticks
 * @param[in]	msecs : The amount of time, in millisecond, that the calling task should block
 * @return	none
 **/
void ms_osal_thread_sleep(int32_t msecs);

/**
 * @brief	run a thread if not autorun
 * @param[in]	thandle : Task structure pointer
 **/
void ms_osal_thread_run(ms_osal_thread_t *thandle);

/**
 * @brief	get thread name
 * @return	the name of current thread
 **/
const char *ms_osal_thread_get_name(void);

/**
 * @brief	get thread stack space
 * @param[in]	thandle : The handle of the task
 * @return	the free memory of thread stack
 **/
uint32_t ms_osal_thread_stack_free(ms_osal_thread_t *thandle);

#ifdef __cplusplus
}
#endif

#endif /* __MS_OSAL_KERNEL_H__ */

